package cn.tedu.bonfire.front.content.schedule;

import cn.tedu.bonfire.front.content.pojo.entity.LikeCollect;
import cn.tedu.bonfire.front.content.pojo.entity.LikeComment;
import cn.tedu.bonfire.front.content.service.ISaveLCInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static cn.tedu.bonfire.common.consts.cache.ArticleLikesConsts.KEY_PREFIX_USER_LIKE;
@Slf4j
@Component
public class LikeCollectCacheSchedule{
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Autowired
    private ISaveLCInfoService saveLCInfoService;
    /**
     * 定时将数据从redis存到数据库
     */
    @Scheduled(fixedRate = 120 * 120 * 1000)
    public void saveDataTODB() {
        String keyPattern = KEY_PREFIX_USER_LIKE + "*:*";
        Set<String> matchingKeys = redisTemplate.keys(keyPattern);
        HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();

        for (String key : matchingKeys) {
            Map<Object, Object> map = opsForHash.entries(key);

            // 定义正则表达式
            Pattern pattern = Pattern.compile(":([0-9]+):");

            // 进行匹配
            Matcher matcher = pattern.matcher(key);

            // 获取匹配到的数字
            if (matcher.find()) {
                String numberStr = matcher.group(1); // 第1个匹配组表示括号中的子表达式
                Long userId = Long.parseLong(numberStr);

                //获取noteId
                Long articleId; // 定义一个 Long 类型的变量 noteId，并初始化为 null

                if (map.get("articleId") != null) {
                    articleId = Long.valueOf(map.get("articleId").toString()); // 如果 map 中的 noteId 不为 null，就进行赋值
                } else {
                    String result = key.substring(key.lastIndexOf(":") + 1);
                    articleId = Long.valueOf(result);
                }

                //获取状态属性，为null则给不处理文章点赞状态
                Object tamp = map.get("state");
                Integer Nstate;
                if (tamp != null) {
                    Nstate = (Integer) tamp;

                    // 根据数据构建新的 文章点赞收藏实体类
                    LikeCollect likeCollect = new LikeCollect();
                    likeCollect.setArticleId(articleId);
                    likeCollect.setUserId(userId);
                    likeCollect.setState(Nstate);

                    // 根据数据库中是否有相应值来确定是更新还是新增
                    if (saveLCInfoService.switchUpdateOrInsertArticle(userId,articleId)) {
                        saveLCInfoService.updateLikeCollection(likeCollect);
                    } else {
                        saveLCInfoService.saveLikeCollection(likeCollect);
                    }
                }

                //去除map中的articleId,state
                map.remove("articleId");
                map.remove("state");
            }
        }
    }
}
